package love.yuanmengrobot.qdkaoqin.business;

import androidx.appcompat.app.AppCompatActivity;

import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.NetworkOnMainThreadException;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ListView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.io.IOException;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import love.yuanmengrobot.qdkaoqin.R;
import love.yuanmengrobot.qdkaoqin.adapter.ClazzAdapter;
import love.yuanmengrobot.qdkaoqin.entity.Clazz;
import love.yuanmengrobot.qdkaoqin.entity.Member;
import love.yuanmengrobot.qdkaoqin.util.Constants;
import love.yuanmengrobot.qdkaoqin.util.TeacherConstants;
import okhttp3.Call;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class StudentHomeActivity extends AppCompatActivity {

    public static final String CURRENT_MEMBER="memberId";
    public static String MEMBER_MESSAGE;
    private String urlMemberiddParams;
    private ClazzAdapter classAdapter;
    private List<Clazz> clazzData;
    private ListView classList;
    private Member member;
    private  Button joinButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home_student);


        Intent intent = getIntent();
        MEMBER_MESSAGE = intent.getStringExtra(ToLoginActivity.MEMBER_MESSAGE);


        Gson gson = new Gson();
        Type jsonType =////////****
                new TypeToken<Member>() {
                }.getType();
        member = gson.fromJson(MEMBER_MESSAGE, jsonType);
        System.out.println("获取:" + member);
        urlMemberiddParams = "?memberid=" + member.getMemberid();

        initView();
        initDate();
//        listItemOnClick();//班级列表添加点击事件
        final String memberid=member.getMemberid();
        ImageButton my=findViewById(R.id.my);
        my.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(StudentHomeActivity.this,PersonaCenter.class).putExtra("memberid",memberid));
            }
        });


    }
    public void initDate() {
        clazzData = new ArrayList<>();
        classAdapter = new ClazzAdapter(StudentHomeActivity.this,
                R.layout.list_teacher_clazz, clazzData);
        classList.setAdapter(classAdapter);
        refreshData(1);
    }
    private void initView() {
        classList = findViewById(R.id.joinClassList);
        classList.setOnItemClickListener(
                new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> adapterView,
                                            View view, int i, long l) {
                        Intent intent = new Intent(StudentHomeActivity.this,
                                StudentClazzDescActivity.class);
                        Clazz clazz = classAdapter.getItem(i);
                        intent.putExtra(TeacherConstants.CLAZZ_ID,
                                clazz.getClazzid());
                        intent.putExtra(TeacherConstants.CLAZZ_NAME,
                                clazz.getClazzname());
                        intent.putExtra(TeacherConstants.MEMBERID,
                                member.getMemberid());
                        startActivity(intent);
                    }
                });
        joinButton = findViewById(R.id.joinNewClass);
        joinButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(StudentHomeActivity.this,
                        JoinClassActivity.class);
                intent.putExtra(TeacherConstants.MEMBERID,
                        member.getMemberid());
                startActivity(intent);
//                finish();
            }
        });














//        Button joinClass=findViewById(R.id.joinNewClass);
//        joinClass.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                startActivity(new Intent(StudentHomeActivity.this, JoinClassActivity.class).putExtra(CURRENT_MEMBER, memberid));
//                finish();
//            }
//        });

    }

    private okhttp3.Callback callback = new okhttp3.Callback() {
        @Override
        public void onFailure(Call call, IOException e) {
//            Log.e(TAG, "Failed to connect server!");
            e.printStackTrace();
        }

        @Override
        public void onResponse(Call call, Response response)
                throws IOException {
            if (response.isSuccessful()) {
                final String body = response.body().string();
                System.out.println("67777777776:" + body);
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Gson gson = new Gson();
                        Type jsonType =////////****
                                new TypeToken<List<Clazz>>() {
                                }.getType();
                        List<Clazz> clazzListResponse =
                                gson.fromJson(body, jsonType);
                        if(clazzListResponse==null)return;
                        for (Clazz cl : clazzListResponse) {
                            classAdapter.add(cl);
                        }
                        classAdapter.notifyDataSetChanged();
                    }
                });
            } else {
            }
        }
    };


    private void refreshData(final int page) {
//?memberid=11
        new Thread(new Runnable() {
            @Override
            public void run() {
                Request request = new Request.Builder()
                        .url(TeacherConstants.CLAZZ_INIT_URL + urlMemberiddParams)
                        .get().build();
                try {
                    OkHttpClient client = new OkHttpClient();
                    client.newCall(request).enqueue(callback);
                } catch (NetworkOnMainThreadException ex) {
                    ex.printStackTrace();
                }
            }
        }).start();
    }

//    public void init(){
////        String memberId=getIntent().getStringExtra(ToLoginActivity.MEMBER_MESSAGE);
////        if(memberId==null){
////            memberId=getIntent().getStringExtra(JoinClassActivity.JOINCLASS_MESSAGE);
////        }
////        final String memberid=memberId;
////        getClasses(memberid);
//
//
//
//        classList=findViewById(R.id.joinClassList);
//
//        clazzData = new ArrayList<>();
//        classAdapter = new ClazzAdapter(StudentHomeActivity.this,
//                R.layout.list_teacher_clazz, clazzData);
//        classList.setAdapter(classAdapter);
//
//
//        //加入班级
//
//    }
//
//    public void listItemOnClick(){
//        classList.setOnItemClickListener(
//                new AdapterView.OnItemClickListener() {
//                    @Override
//                    public void onItemClick(AdapterView<?> adapterView,
//                                            View view, int i, long l) {
//                        Intent intent = new Intent(StudentHomeActivity.this, StudentClassDescActivity.class);
//                        Clazz clazz = classAdapter.getItem(i);
//                        intent.putExtra(Constants.CLAZZ_ID,
//                                clazz.getClazzid());
//                        intent.putExtra(Constants.CLAZZ_NAME,
//                                clazz.getClazzname());
//                        startActivity(intent);
//                    }
//                });
//    }
//
//    public void getClasses(final String memberId){
//        new Thread(new Runnable() {
//            @Override
//            public void run() {
//                String urlParams = "?memberid="+memberId;//参数
//                Request request = new Request.Builder()
//                        .url(Constants.CLASSINIT_URL + urlParams)
//                        .get().build();
//                try {
//                    OkHttpClient client = new OkHttpClient();
//                    client.newCall(request).enqueue(callback);
//                } catch (NetworkOnMainThreadException ex) {
//
//                    ex.printStackTrace();
//                }
//            }
//        }).start();
//
//    }
//
//    private okhttp3.Callback callback = new okhttp3.Callback() {
//        @Override
//        public void onFailure(Call call, IOException e) {
////            Log.e(TAG, "Failed to connect server!");
//            e.printStackTrace();
//        }
//
//        @Override
//        public void onResponse(Call call, Response response) throws IOException {
//            if (response.isSuccessful()) {
//                final String body = response.body().string();
//                System.out.println(body);
//                runOnUiThread(new Runnable() {
//                    @Override
//                    public void run() {
//                        Gson gson = new Gson();
//                        Type jsonType =
//                                new TypeToken<List<Clazz>>() {
//                                }.getType();
//                        List<Clazz> temp =gson.fromJson(body, jsonType);
//                        for (Clazz c:temp){
//                            classAdapter.add(c);
//                        }
//                        classAdapter.notifyDataSetChanged();
//                    }
//                });
//            } else {
//                final AlertDialog alertDialog1 = new AlertDialog.Builder(StudentHomeActivity.this)
//                        .setTitle("错误！")//标题
//                        .setMessage("服务请求错误！")//内容
//                        .setIcon(R.mipmap.ic_warning)
//                        .create();
//                alertDialog1.show();
//                final Timer t=new Timer();//设置警告窗口自动关闭
//                t.schedule(new TimerTask() {
//                    @Override
//                    public void run() {
//                        alertDialog1.dismiss();
//                        t.cancel();
//                    }
//                },2000);//2秒后警告窗口自动关闭
//            }
//        }
//    };
//
//    //刷新
//    public void reflush(){
//        Intent intent = getIntent();
//        finish();
//        startActivity(intent);
//    }
}